package com.fingard.dsp.bank.directbank.icbc02;

import com.fingard.FGBiz;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.ITransaction;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7515TodayTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7516HisTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.RetAtsTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.detail.RetAtsTransDetail;
import com.fingard.text.StringHelper;
import com.fingard.util.DateHelper;
import com.fingard.util.FtpUtil;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Calendar;
import java.util.Date;

public class ICBC02Transaction extends ICBC02Base implements ITransaction {
    public RetAtsTrans d_7516_HisTrans(Req7516HisTrans p_req7516) throws Exception {
        RetAtsTrans retAts = new RetAtsTrans(p_req7516.reqHeader);
        String tmpRespCode = "";
        String tmpRespInfo = "";
        retAts.writeStart();

        try {
            //回单是否拼接账号
            boolean isReceiptJoinAct = false;
            if (!"pro".equalsIgnoreCase(getBankConfig().getValueAsString("receiptVersion")) && getBankConfig().getValueAsBool("isHeadVersion")) {
                isReceiptJoinAct = true;
            }

            int qryCount = 0;
            String nextTag = "";
            boolean isSubAct = p_req7516.actBankType.equals("5");
            while (tmpRespCode.length() == 0) {
                qryCount++;
                bnkPackageID = p_req7516.reqHeader.transSeqID + String.valueOf(qryCount);
                if (bnkPackageID.length() > 35) {
                    bnkPackageID = bnkPackageID.substring(bnkPackageID.length() - 35, bnkPackageID.length());
                }
                bnkfSeqno = bnkPackageID;
                bnkTransCode = "QHISD";
                XmlTextWriter xmlWriter = new XmlTextWriter();
                writeCommonHeader(xmlWriter);
                //获取版本号
                String version = getBankConfig().getMapValueByKey("versionMapping", bnkTransCode);
                version = "".equals(version) ? "0.0.0.1" : version;

                if (!isSubAct && "0.0.0.1".equals(version)) {
                    xmlWriter.writeElementString("AccNo", p_req7516.actNum);
                    xmlWriter.writeElementString("BeginDate", p_req7516.dateFrom);
                    xmlWriter.writeElementString("EndDate", p_req7516.dateTo);
                    xmlWriter.writeElementString("MinAmt", "0");//历史明细不区分正负数，必须大于等于0
                    xmlWriter.writeElementString("MaxAmt", "99999999999999999");
                    xmlWriter.writeElementString("NextTag", nextTag, true);
                    xmlWriter.writeElementClosed("ReqReserved1");
                    xmlWriter.writeElementClosed("ReqReserved2");
                } else {
                    bnkVersion = "0.0.1.0";
                    String acctNo = p_req7516.higherLevelAct;
                    if (StringHelper.isNullOrEmpty(acctNo)) {
                        acctNo = p_req7516.actNum;
                    }
                    xmlWriter.writeElementString("AccNo", acctNo);
                    xmlWriter.writeElementString("BeginDate", p_req7516.dateFrom);

                    boolean removeToday7516 = getBankConfig().getValueAsBool("removeToday7516");
                    WriteBankLogLn2("历史明细查询是否限制今日明细查询(removeToday7516):" + removeToday7516);
                    if (removeToday7516) {
                        Date tmpDateTo = Format.Date8Format.parse(p_req7516.dateTo);
                        Date tmpDateNow = new Date();
                        if (DateHelper.compareDay(tmpDateTo, tmpDateNow) >= 0) {
                            Calendar calendar = Calendar.getInstance();
                            calendar.setTime(tmpDateNow);
                            calendar.add(Calendar.DATE, -1);
                            p_req7516.dateTo = Format.Date8Format.format(calendar.getTime());
                        }
                    }

                    xmlWriter.writeElementString("EndDate", p_req7516.dateTo);
                    xmlWriter.writeElementString("MinAmt", "0");//历史明细不区分正负数，必须大于等于0
                    xmlWriter.writeElementString("MaxAmt", "99999999999999999");
                    xmlWriter.writeElementClosed("BankType");
                    xmlWriter.writeElementString("NextTag", nextTag, true);
                    xmlWriter.writeElementClosed("CurrType");
                    xmlWriter.writeElementClosed("DueBillNo");
                    xmlWriter.writeElementString("AcctSeq", "");
                    xmlWriter.writeElementClosed("ComplementFlag");
                }
                xmlWriter.writeEndDocument();

                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

                XmlTextReader tmpBkReader;
                String tmpAccNo = "";
                String tmpCurrType = "";
                String tmpAtsCur = "";
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String tmpRetCode = tmpBkReader.getFirstText("/CMS/eb/pub/RetCode");
                        String tmpRetMsg = tmpBkReader.getFirstText("/CMS/eb/pub/RetMsg");
                        if (isActiveRetCode(tmpRetCode)) {
                            nextTag = tmpBkReader.getFirstText("/CMS/eb/out/NextTag");
                            WriteBankLogLn("当前nextTg:" + nextTag);
                            nextTag = antiTransferStr(nextTag);
                            WriteBankLogLn("反转义后nextTg:" + nextTag);

                            tmpAccNo = tmpBkReader.getFirstText("/CMS/eb/out/AccNo");
                            tmpCurrType = tmpBkReader.getFirstText("/CMS/eb/out/CurrType");
                            tmpAtsCur = convertCurToATS(tmpCurrType);
                            if (!tmpAccNo.equals(p_req7516.actNum)) {
                                tmpRespCode = "-1";
                                tmpRespInfo = "银行返回的账号" + tmpAccNo + "与请求账号" + p_req7516.actNum + "不符";
                                break;
                            }
                            String tmpTotalNum = tmpBkReader.getFirstText("/CMS/eb/out/TotalNum");
                            if (StringHelper.isNullOrEmpty(tmpTotalNum)) {
                                tmpRespCode = "-1";
                                tmpRespInfo = "银行返回的交易条数为空";
                                break;
                            }
                        } else if (tmpRetCode.equals("B0116") && qryCount == 1) {
                            tmpRespCode = "0";
                            tmpRespInfo = tmpRetMsg;
                        } else {
                            tmpRespCode = "-1";
                            tmpRespInfo = tmpRetMsg;
                            break;
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpSbMsg.toString();
                        break;
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpStrRet[1];
                    break;
                }
                while (tmpBkReader.read("/CMS/eb/out/rd")) {
                    RetAtsTransDetail tmpRetDetail = new RetAtsTransDetail();
                    tmpRetDetail.actNum = tmpAccNo;
                    tmpRetDetail.recAccNum = tmpBkReader.readedXElement.getSingleText("RecipAccNo");
                    tmpRetDetail.recAccName = tmpBkReader.readedXElement.getSingleText("RecipName");

                    String recAccBank = tmpBkReader.readedXElement.getSingleText("RecipBkName");
                    if (StringHelper.isNullOrEmpty(recAccBank) || "00000".equals(recAccBank)) {
                        recAccBank = tmpBkReader.readedXElement.getSingleText("RecipBkName1");
                    }
                    tmpRetDetail.recAccBank = recAccBank;
                    if (StringHelper.isNullOrEmpty(tmpRetDetail.recAccBank)) {
                        tmpRetDetail.recAccBank = tmpBkReader.readedXElement.getSingleText("RecipBkNo");
                    }
                    ///CMS/eb/out/rd/Drcrf	借贷标志	否	数字	1	1:借 2:贷
                    String tmpDrcrf = tmpBkReader.readedXElement.getSingleText("Drcrf");
                    if (tmpDrcrf.trim().equals("1")) {
                        String tmpDebitAmount = tmpBkReader.readedXElement.getSingleText("DebitAmount").trim();
                        if (tmpDebitAmount.startsWith("-")) {
                            //借负，银行退票,表示收入。
                            tmpRetDetail.tranAmt = StringHelper.amountDivide100(tmpDebitAmount.substring(1));
                            tmpRetDetail.cdFlag = "2";//借贷表示至为贷，表示收入
                        } else {
                            //借正，正常业务
                            tmpRetDetail.tranAmt = StringHelper.amountDivide100(tmpDebitAmount);
                            tmpRetDetail.cdFlag = "1";
                        }
                    } else if (tmpDrcrf.trim().equals("2")) {
                        String tmpCreditAmount = tmpBkReader.readedXElement.getSingleText("CreditAmount").trim();
                        if (tmpCreditAmount.startsWith("-")) {
                            //贷负，目前应该没有这种业务，按照正常的程序逻辑做一下处理。
                            tmpRetDetail.tranAmt = StringHelper.amountDivide100(tmpCreditAmount.substring(1));
                            tmpRetDetail.cdFlag = "1";//借贷表示至为借，表示支出
                        } else {
                            //贷正，正常业务
                            tmpRetDetail.tranAmt = StringHelper.amountDivide100(tmpCreditAmount);
                            tmpRetDetail.cdFlag = "2";
                        }

                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = "借贷标志Drcrf有误：" + tmpDrcrf;
                        break;
                    }
                    String tmpDate = tmpBkReader.readedXElement.getSingleText("Date");
                    if (tmpDate.length() == 8) {
                        tmpRetDetail.tranDate = tmpDate.substring(0, 4) + "-" + tmpDate.substring(4, 6) + "-" + tmpDate.substring(6, 8);
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = "交易日期Date格式有误：" + tmpDate;
                        break;
                    }
                    String tmpTime = tmpBkReader.readedXElement.getSingleText("Time");
                    tmpRetDetail.postTimestamp = tmpTime;
                    //他行往来户历史明细有该字段,格式为HH.MM.SS；（实际是交易时间）;
                    if (tmpTime.length() == 8) {
                        tmpTime = tmpRetDetail.tranDate + "-" + tmpTime;
                    }
                    if (tmpTime.length() > 18) {
                        tmpRetDetail.tranTime = tmpTime.substring(0, 10) + " " + tmpTime.substring(11, 19).replace('.', ':');
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = "时间戳Time格式有误：" + tmpTime;
                        break;
                    }
                    tmpRetDetail.valueDate = tmpRetDetail.tranDate;
                    if (!StringHelper.isNullOrEmpty(tmpBkReader.readedXElement.getSingleText("Balance"))) {
                        tmpRetDetail.accBal = StringHelper.amountDivide100(tmpBkReader.readedXElement.getSingleText("Balance"));
                    }

                    tmpRetDetail.billNum = tmpBkReader.readedXElement.getSingleText("VouhNo");
                    tmpRetDetail.billType = tmpBkReader.readedXElement.getSingleText("VouhType");
                    if (StringHelper.isNullOrEmpty(tmpRetDetail.billType)) {
                        tmpRetDetail.billType = tmpBkReader.readedXElement.getSingleText("BusType");
                    }

                    tmpRetDetail.comment = tmpBkReader.readedXElement.getSingleText("Summary");
                    tmpRetDetail.cur = tmpAtsCur;
                    tmpRetDetail.seqId = tmpBkReader.readedXElement.getSingleText("RepReserved3");
                    tmpRetDetail.originator = tmpBkReader.readedXElement.getSingleText("VouhNo");
                    tmpRetDetail.urid = p_req7516.actNum + tmpBkReader.readedXElement.getSingleText("OnlySequence");
                    tmpRetDetail.payCode = tmpBkReader.readedXElement.getSingleText("PostScript");
                    if (!getBankConfig().getValueAsBool("qryTransPayCodeOnlyPostscript")) {
                        if (StringHelper.isNullOrEmpty(tmpRetDetail.payCode)) {
                            tmpRetDetail.payCode = tmpBkReader.readedXElement.getSingleText("Ref");
                        }
                        if (StringHelper.isNullOrEmpty(tmpRetDetail.payCode)) {
                            tmpRetDetail.payCode = tmpBkReader.readedXElement.getSingleText("Oref");
                        }
                        if (StringHelper.isNullOrEmpty(tmpRetDetail.payCode)) {
                            tmpRetDetail.payCode = tmpBkReader.readedXElement.getSingleText("RepReserved4");
                        }
                    }

                    tmpRetDetail.abstr = pickupAbstr(tmpBkReader.readedXElement.getSingleText("PostScript"), tmpBkReader.readedXElement.getSingleText("Summary"), tmpBkReader.readedXElement.getSingleText("UseCN"));
                    String originator = tmpBkReader.readedXElement.getSingleTextNotNull("RepReserved3");
                    String[] originatorStrs = originator.split("\\|");
                    if (originatorStrs.length > 3) {
                        tmpRetDetail.originator = originatorStrs[2] + originatorStrs[3];
                    }
                    if (isReceiptJoinAct && !StringHelper.isNullOrEmpty(tmpBkReader.readedXElement.getSingleText("RepReserved4"))) {
                        tmpRetDetail.receiptNo = tmpAccNo + "R" + tmpBkReader.readedXElement.getSingleText("RepReserved4");
                    } else {
                        if (tmpBkReader.readedXElement.getSingleTextNotNull("OnlySequence").length() > 0) {
                            tmpRetDetail.receiptNo = tmpBkReader.readedXElement.getSingleTextNotNull("OnlySequence");
                        } else {
                            tmpRetDetail.receiptNo = tmpBkReader.readedXElement.getSingleTextNotNull("RepReserved4");
                        }
                    }

                    boolean enableRefund = getBankConfig().getValueAsBool("enableRefund");
                    if (enableRefund) {
                        Connection conn = FGBiz.getLocalDbConn();
                        Statement statement = null;
                        ResultSet rs = null;
                        try {
                            statement = conn.createStatement();
                            String refundMsg = tmpBkReader.readedXElement.getSingleText("RefundMsg");
                            if (StringHelper.hasAnyChar(refundMsg)) {
                                tmpRetDetail.usage = refundMsg;
                                tmpRetDetail.comment = refundMsg;
                            }

                            String tmpReceiptInfo = tmpBkReader.readedXElement.getSingleText("ReceiptInfo");
                            String ref1 = tmpBkReader.readedXElement.getSingleText("Ref1");
                            if (StringHelper.hasAnyChar(ref1)) {
                                tmpRetDetail.askNumber = ref1;
                                if (tmpReceiptInfo.contains("&lt;支付交易序号")) {
                                    //<支付交易序号>99686273</支付交易序号>
                                    int index1 = tmpReceiptInfo.indexOf("&lt;支付交易序号") + 14;
                                    String bkTransPaySeq = tmpReceiptInfo.substring(index1, tmpReceiptInfo.indexOf("&lt;/支付交易序号"));
                                    String upSql = "update ICBCPAYITEM set bkTransSeq='" + bkTransPaySeq + "' where asknumber='" + ref1 + "'";
                                    statement.executeUpdate(upSql);
                                }
                            }

                            if (StringHelper.hasAnyChar(refundMsg)) {
                                if (tmpReceiptInfo.contains("&lt;原支付交易序号")) {
                                    int index1 = tmpReceiptInfo.indexOf("&lt;原支付交易序号") + 15;
                                    String oriBkTransSeq = tmpReceiptInfo.substring(index1, tmpReceiptInfo.indexOf("&lt;/原支付交易序号"));
                                    String queryStr = "select * from ICBCPAYITEM where bkTransSeq='" + oriBkTransSeq + "'";
                                    rs = statement.executeQuery(queryStr);
                                    while (rs.next()) {
                                        tmpRetDetail.askNumber = rs.getString("asknumber");
                                        String amout = rs.getString("amount").trim();
                                        String bankAmout = tmpBkReader.readedXElement.getSingleText("DebitAmount").trim();
                                        if (amout.equalsIgnoreCase(bankAmout)) {
                                            String refundDate = Format.Date10Format.format(Format.Date8Format.parse(tmpBkReader.readedXElement.getSingleText("Date")));
                                            String upSql = "update ICBCPAYITEM set isrefund=1,refunddate='" + refundDate + "',refundreason='" + refundMsg.trim() + "' where bkTransSeq='" + oriBkTransSeq + "'";
                                            statement.executeUpdate(upSql);
                                            break;
                                        }
                                    }
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {
                            if (statement != null) {
                                statement.close();
                            }
                            if (rs != null) {
                                rs.close();
                            }
                        }
                    }
                    nodeAnalysic(tmpBkReader.readedXElement, tmpRetDetail, getCharset());

                    retAts.writeRetDetail(tmpRetDetail);
                    if ("receiptXml".equals(getBankConfig().getValueAsString("receiptType"))) {
                        generateHisTranFile(tmpRetDetail, tmpBkReader.readedXElement.getSingleText("RepReserved4"));
                    }
                }
                if (StringHelper.isNullOrEmpty(nextTag)) {
                    break;
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRespCode = "-1";
            tmpRespInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }
        if ("-1".equals(tmpRespCode)) {
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("交易为空");
        }
        retAts.writeEnd();
        return retAts;
    }

    public RetAtsTrans d_7515_TodayTrans(Req7515TodayTrans p_req7515) throws Exception {
        String version = getBankConfig().getMapValueByKey("versionMapping", bnkTransCode);
        if(StringHelper.hasAnyChar(version) && "0.0.1.0".equals(version)){
            Req7516HisTrans tmpReq7516 = new Req7516HisTrans(p_req7515.reqHeader);
            tmpReq7516.actArea = p_req7515.actArea;
            tmpReq7516.actCur = p_req7515.actCur;
            tmpReq7516.actNum = p_req7515.actNum;
            String tmpStrToday = Format.Date8Format.format(new Date());
            tmpReq7516.dateFrom = tmpStrToday;
            tmpReq7516.dateTo = tmpStrToday;
            return d_7516_HisTrans(tmpReq7516);
        }
        //历史明细查询返回ATS的对象
        RetAtsTrans retAts = new RetAtsTrans(p_req7515.reqHeader);
        String tmpRespCode = "";
        String tmpRespInfo = "";
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            String savingType = p_req7515.savingType;
            //保证金账户
            if (savingType != null && savingType.equals("3")) {
                bnkTransCode = "QFBPD";
            } else {
                bnkTransCode = "QPD";
            }
            bnkVersion = "0.0.1.0";
            int qryCount = 0;
            String nextTag = "";

            boolean isSubAct = p_req7515.actBankType.equals("5");

            while (tmpRespCode.length() == 0) {
                qryCount++;
                bnkPackageID = p_req7515.reqHeader.transSeqID + String.valueOf(qryCount);
                if (bnkPackageID.length() > 35) {
                    bnkPackageID = bnkPackageID.substring(bnkPackageID.length() - 35, bnkPackageID.length());
                }
                bnkfSeqno = bnkPackageID;

                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入ICBC02通用的头部节点，<in>之后
                writeCommonHeader(xmlWriter);
                        /*
/CMS/eb/in/AccNo
/CMS/eb/in/AreaCode
/CMS/eb/in/MinAmt
/CMS/eb/in/MaxAmt
/CMS/eb/in/BeginTime
/CMS/eb/in/EndTime
/CMS/eb/in/NextTag
/CMS/eb/in/ReqReserved1
/CMS/eb/in/ReqReserved2

						 * */
                xmlWriter.writeElementString("AccNo", isSubAct ? p_req7515.higherLevelAct : p_req7515.actNum);
                if (bnkTransCode.equals("QFBPD")) {
                    xmlWriter.writeElementString("CurrType", convertCurToBank(p_req7515.actCur));
                }
                xmlWriter.writeElementString("AreaCode", "");
                xmlWriter.writeElementString("MinAmt", "0");//银行区分正负号
                xmlWriter.writeElementString("MaxAmt", "0");
                if (bnkTransCode.equals("QPD")) {
                    xmlWriter.writeElementString("BeginTime", "000000");
                    xmlWriter.writeElementString("EndTime", "235959");
                }
                xmlWriter.writeElementString("NextTag", nextTag, true);
                xmlWriter.writeElementString("ReqReserved1", "");
                xmlWriter.writeElementString("ReqReserved2", "");
                if (bnkTransCode.equals("QPD")) {
                    xmlWriter.writeElementString("CurrType", convertCurToBank(p_req7515.actCur));
                    xmlWriter.writeElementString("AcctSeq", isSubAct ? p_req7515.actNum : "");
                }
                xmlWriter.writeEndDocument();
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                WriteBankLogStep3(tmpStrRet[1]);
                XmlTextReader tmpBkReader;
                //int tmpRetRdNum = -1; ///
                String tmpAccNo = "";
                String tmpCurrType = "";
                String tmpAtsCur = "";
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String tmpRetCode = tmpBkReader.getFirstText("/CMS/eb/pub/RetCode");
                        String tmpRetMsg = tmpBkReader.getFirstText("/CMS/eb/pub/RetMsg");
                        if (isActiveRetCode(tmpRetCode)) {
                            nextTag = tmpBkReader.getFirstText("/CMS/eb/out/NextTag");
                            tmpAccNo = tmpBkReader.getFirstText("/CMS/eb/out/AccNo");
                            tmpCurrType = tmpBkReader.getFirstText("/CMS/eb/out/CurrType");
                            tmpAtsCur = convertCurToATS(tmpCurrType);
                            if (!tmpAccNo.equals(p_req7515.actNum)) {
                                tmpRespCode = "-1";
                                tmpRespInfo = "银行返回的账号" + tmpAccNo + "与请求账号" + p_req7515.actNum + "不符";
                                break;
                            }
                            String tmpTotalNum = tmpBkReader.getFirstText("/CMS/eb/out/TotalNum");
                            //if(!StringHelper.isNullOrEmpty(tmpTotalNum)){
                            //tmpRetRdNum = Integer.parseInt(tmpTotalNum);
                            //}
                            //else{
                            if (StringHelper.isNullOrEmpty(tmpTotalNum)) {
                                tmpRespCode = "-1";
                                tmpRespInfo = "银行返回的交易条数为空";
                                break;
                            }
                        } else if (tmpRetCode.equals("B0116") && qryCount == 1) {
                            tmpRespCode = "0";
                            tmpRespInfo = tmpRetMsg;
                            //retAts.writeRespCode("0");
                            //retAts.writeRespInfo(tmpRetMsg);
                        } else {
                            tmpRespCode = "-1";
                            tmpRespInfo = tmpRetMsg;
                            break;
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpSbMsg.toString();
                        break;
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpStrRet[1];
                    break;
                }
                //if(qryCount == 1){
                //	retAts.writeRespCode("0");
                //	if(tmpRetRdNum>0){
                //		retAts.writeRespInfo("");
                //	}
                //	else{
                //		retAts.writeRespInfo("交易为空");
                //	}
                //}
                while (tmpBkReader.read("/CMS/eb/out/rd")) {
                    RetAtsTransDetail tmpRetDetail = new RetAtsTransDetail();
                    tmpRetDetail.actNum = tmpAccNo;
                    tmpRetDetail.recAccNum = tmpBkReader.readedXElement.getSingleText("RecipAccNo");
                    tmpRetDetail.recAccName = tmpBkReader.readedXElement.getSingleText("RecipName");
                    tmpRetDetail.recAccBank = tmpBkReader.readedXElement.getSingleText("RecipBkNo");
                    //if(StringHelper.isNullOrEmpty(tmpRetDetail.recAccBank)){
                    //	tmpRetDetail.recAccBank = tmpBkReader.readedXElement.getSingleText("RecipBkNo");
                    //}
                    ///CMS/eb/out/rd/Drcrf	借贷标志	否	数字	1	1:借 2:贷
                    String tmpDrcrf = tmpBkReader.readedXElement.getSingleText("Drcrf");
                    String tmpBkAmount = tmpBkReader.readedXElement.getSingleText("Amount");
                    if (tmpDrcrf.trim().equals("1")) {
                        if (tmpBkAmount.startsWith("-")) {
                            //借负，银行退票,表示收入。
                            tmpRetDetail.tranAmt = StringHelper.amountDivide100(tmpBkAmount.substring(1));
                            tmpRetDetail.cdFlag = "2";//借贷表示至为贷，表示收入
                        } else {
                            //借正，正常业务
                            tmpRetDetail.tranAmt = StringHelper.amountDivide100(tmpBkAmount);
                            tmpRetDetail.cdFlag = "1";
                        }
                    } else if (tmpDrcrf.trim().equals("2")) {
                        if (tmpBkAmount.startsWith("-")) {
                            //贷负，目前应该没有这种业务，按照正常的程序逻辑做一下处理。
                            tmpRetDetail.tranAmt = StringHelper.amountDivide100(tmpBkAmount.substring(1));
                            tmpRetDetail.cdFlag = "1";//借贷表示至为借，表示支出
                        } else {
                            //贷正，正常业务
                            tmpRetDetail.tranAmt = StringHelper.amountDivide100(tmpBkAmount);
                            tmpRetDetail.cdFlag = "2";
                        }

                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = "借贷标志Drcrf有误：" + tmpDrcrf;
                        break;
                    }

                    //<Time>2012-09-10-00.45.34.597322</Time>
                    String tmpTime = tmpBkReader.readedXElement.getSingleText("TimeStamp");
                    tmpRetDetail.postTimestamp = tmpTime;
                    if (tmpTime.length() > 18) {
                        tmpRetDetail.tranDate = tmpTime.substring(0, 10);
                        tmpRetDetail.tranTime = tmpTime.substring(0, 10) + " " + tmpTime.substring(11, 19).replace('.', ':');
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = "时间戳Time格式有误：" + tmpTime;
                        break;
                    }
                    tmpRetDetail.valueDate = tmpRetDetail.tranDate;
                    if (!StringHelper.isNullOrEmpty(tmpBkReader.readedXElement.getSingleText("Balance"))) {
                        tmpRetDetail.accBal = StringHelper.amountDivide100(tmpBkReader.readedXElement.getSingleText("Balance"));
                    }

                    tmpRetDetail.billNum = tmpBkReader.readedXElement.getSingleText("VouhNo");
                    tmpRetDetail.billType = tmpBkReader.readedXElement.getSingleText("CvouhType");
                    if (StringHelper.isNullOrEmpty(tmpRetDetail.billType)) {
                        tmpRetDetail.billType = tmpBkReader.readedXElement.getSingleText("BusType");
                    }

                    tmpRetDetail.comment = tmpBkReader.readedXElement.getSingleText("Summary");
                    tmpRetDetail.usage = tmpBkReader.readedXElement.getSingleText("UseCN");
                    tmpRetDetail.abstr = pickupAbstr(tmpBkReader.readedXElement.getSingleText("PostScript"), tmpBkReader.readedXElement.getSingleText("Summary"), tmpBkReader.readedXElement.getSingleText("UseCN"));
                    tmpRetDetail.cur = tmpAtsCur;

                    tmpRetDetail.seqId = tmpBkReader.readedXElement.getSingleText("RepReserved3");
                    tmpRetDetail.originator = tmpBkReader.readedXElement.getSingleText("VouhNo");

                    String originator = tmpBkReader.readedXElement.getSingleTextNotNull("RepReserved3");
                    String[] originatorStrs = originator.split("\\|");
                    if (originatorStrs.length > 3) {
                        tmpRetDetail.originator = originatorStrs[2] + originatorStrs[3];
                    }
                    tmpRetDetail.payCode = tmpBkReader.readedXElement.getSingleText("PostScript");
                    if (!getBankConfig().getValueAsBool("qryTransPayCodeOnlyPostscript")) {
                        if (StringHelper.isNullOrEmpty(tmpRetDetail.payCode)) {
                            tmpRetDetail.payCode = tmpBkReader.readedXElement.getSingleText("Ref");
                        }
                        if (StringHelper.isNullOrEmpty(tmpRetDetail.payCode)) {
                            tmpRetDetail.payCode = tmpBkReader.readedXElement.getSingleText("Oref");
                        }
                        if (StringHelper.isNullOrEmpty(tmpRetDetail.payCode)) {
                            tmpRetDetail.payCode = tmpBkReader.readedXElement.getSingleText("RepReserved4");
                        }
                    }

                    nodeAnalysic(tmpBkReader.readedXElement, tmpRetDetail, getCharset());
                    if (tmpBkReader.readedXElement.getSingleTextNotNull("OnlySequence").length() > 0) {
                        tmpRetDetail.receiptNo = tmpBkReader.readedXElement.getSingleTextNotNull("OnlySequence");
                    } else {
                        tmpRetDetail.receiptNo = tmpBkReader.readedXElement.getSingleTextNotNull("RepReserved4");
                    }
                    retAts.writeRetDetail(tmpRetDetail);
                }
                if (StringHelper.isNullOrEmpty(nextTag)) {
                    break;
                }
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if (tmpRespCode.equals("-1")) {
            retAts = new RetAtsTrans(p_req7515.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("交易为空");
        }
        retAts.writeEnd();
        return retAts;
    }

    private void generateHisTranFile(RetAtsTransDetail tmpRetDetail, String askNumber) throws Exception {
        FileOutputStream fos = null;
        try {
            String tmpByDate = Format.Date8Format.format(Format.Date10Format.parse(tmpRetDetail.tranDate));
            tmpByDate = tmpByDate.substring(0, 6) + File.separator + tmpByDate.substring(6, 8);
            String filePath = getBankConfig().getValueAsString("hisTranFile");
            if (StringHelper.isNullOrEmpty(filePath)) {
                filePath = FGBiz.dspCenter.dspSet.dataDirPath + File.separator + "tempfile" + File.separator + "hisTranFile" + File.separator;
            }
            filePath = filePath + tmpByDate + File.separator + reqHeader.bankCode + File.separator + tmpRetDetail.actNum + ".txt";
            File batFile = new File(filePath);
            if (!batFile.getParentFile().exists()) {
                batFile.getParentFile().mkdirs();
            }
            String fileContent = "";
            String newFileContent = tmpRetDetail.actNum + "|" + tmpRetDetail.tranDate + "|" + askNumber + "|" + tmpRetDetail.seqId;
            if (!batFile.exists()) {
                batFile.createNewFile();
            } else {
                fileContent = FtpUtil.getTextFromFile(filePath, getCharset());
                if (fileContent.contains(newFileContent)) {
                    return;
                }
            }

            fos = new FileOutputStream(batFile);
            fos.write(fileContent.concat(newFileContent).getBytes());
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                fos.close();
            }
        }
    }
}
